﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vuonthieunhi.Areas.Administration.Models;
using vuonthieunhi.Library;
using vuonthieunhi.Models;

namespace TSV.Areas.Administration.Controllers
{
    public class ArticleController : BaseController
    {
        ArticleDao AD = new ArticleDao();
        public ActionResult GetListJQGrid()
        {

            #region JQGrid Params

            string sortColumn = (Request.Params[Constants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[Constants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[Constants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[Constants.ROW_COUNT]);
            #endregion

            List<sp_GetArticleResult> ADList = AD.GetList().ToList<sp_GetArticleResult>();

            int totalRecords = ADList.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);

            var finalList = AD.Sort(ADList, sortColumn, sortOrder)
                                  .Skip((pageIndex - 1) * rowCount)
                                   .Take(rowCount);
            int j = 0;

            var jsonData = new
            {
                total = totalPages,
                page = pageIndex,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {

                        i = m.ID,
                        cell = new string[] {
                                Convert.ToString(j+=1),
                                m.ID.ToString(), 
                                TooltipLink("/Administration/Article/Edit/"+m.ID.ToString(),HttpUtility.HtmlEncode(m.Title)),
                                HttpUtility.HtmlEncode(m.Alias),
                                Constants.ButtonEdit("/Administration/Article/Edit/"+m.ID.ToString()),
                                
                            }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        private string TooltipLink(string actionUrl, string textLink)
        {
            string onclick = string.Format("window.location='{0}';", actionUrl);
            string jsvoid = "javascript:void(0);";
            string str= string.Format("<a id=\"textlnk\" href=\"{0}\" class=\"showTooltip\" onclick=\"{1}\">{2}</a>", jsvoid, onclick, textLink);
            return str;
        }
       
        //
        // GET: /Administration/Article/
        [ValidateInput(false)]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Article ar, FormCollection collection)
        {
            ArticleDao dao = new ArticleDao();
            Message msg = null;
            JsonResult result = new JsonResult();
            result.ContentType = "text/html";
            result.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
            msg = dao.Insert(ar);
            ShowMessage(msg);
            result.Data = msg;
            return result;
            
        }
        public ActionResult DeleteList(string id)
        {
            Message msg = AD.DeleteList(id);
            ShowMessage(msg);
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            Article oscObj = AD.GetById(id.ToString());
            return View(oscObj);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Article oscObj, FormCollection collection)
        {
            ArticleDao dao = new ArticleDao();
            Message msg = null;
            JsonResult result = new JsonResult();
            result.ContentType = "text/html";
            result.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
            try
            {
               
                // TODO: Add update logic here
                msg = AD.Update(oscObj);
       
            }
            catch
            {
                ShowMessage(msg);
                return View();
            }
            ShowMessage(msg);
            result.Data = msg;
            return result;
        }
    }
}
